package com.bootdo.staticmask.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.bootdo.system.controller.LoginController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.staticmask.core.bean.ColumnProperty;
import com.bootdo.staticmask.core.oracle.OracleDataSource;
import com.bootdo.staticmask.domain.MaskOracleDO;
import com.bootdo.staticmask.service.MaskOracleService;

import io.swagger.annotations.ApiOperation;

/**
 * 数据源
 * 
 * @author lkg
 * @email lkggod@163.com
 * @date 2018-04-25 11:27:35
 */
 
@Controller
@RequestMapping("/staticmask/maskOracle")
public class MaskOracleController {

	private final MaskOracleService maskOracleService;

	@Autowired
	public MaskOracleController(MaskOracleService maskOracleService) {
		this.maskOracleService = maskOracleService;
	}


	@GetMapping()
	@RequiresPermissions("staticmask:maskOracle:maskOracle")
	String MaskOracle(){
	    return "staticmask/maskOracle/maskOracle";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("staticmask:maskOracle:maskOracle")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<MaskOracleDO> maskOracleList = maskOracleService.list(query);
		int total = maskOracleService.count(query);
		PageUtils pageUtils = new PageUtils(maskOracleList, total);
		return pageUtils;
	}

	@GetMapping("/add")
	@RequiresPermissions("staticmask:maskOracle:add")
	String add(){
	    return "staticmask/maskOracle/add";
	}

	@GetMapping("/edit/{oracleId}")
	@RequiresPermissions("staticmask:maskOracle:edit")
	String edit(@PathVariable("oracleId") Integer oracleId,Model model){
		MaskOracleDO maskOracle = maskOracleService.get(oracleId);
		model.addAttribute("maskOracle", maskOracle);
	    return "staticmask/maskOracle/edit";
	}

	@ResponseBody
	@PostMapping("/check")
	public R check(MaskOracleDO maskOracleDO){
		String result = OracleDataSource.testConnection(maskOracleDO);
		if ("success".equals(result)){
			return R.ok();
		}
		return R.error(result);
	}
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("staticmask:maskOracle:add")
	public R save( MaskOracleDO maskOracle){
		maskOracle.setCreatetime(new Date());
		maskOracle.setCreateuser(LoginController.getUSER());
		if(maskOracleService.save(maskOracle)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("staticmask:maskOracle:edit")
	public R update( MaskOracleDO maskOracle){
		maskOracle.setCreatetime(new Date());
		maskOracle.setCreateuser(LoginController.getUSER());
		maskOracleService.update(maskOracle);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("staticmask:maskOracle:remove")
	public R remove( Integer oracleId){
		if(maskOracleService.remove(oracleId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("staticmask:maskOracle:remove")
	public R remove(@RequestParam("ids[]") Integer[] oracleIds){
		maskOracleService.batchRemove(oracleIds);
		return R.ok();
	}
	
	@ApiOperation("获取某oracle数据源下所有表")
	@GetMapping("/tables/{oracleSourceId}")
	@ResponseBody
	public R getTables(@PathVariable Integer oracleSourceId) {
		List<Map<String, String>> tables = maskOracleService.getTables(oracleSourceId);
		if (tables != null) {
			R r = new R();
			r.put("tables", tables);
			return r;
		}
		
		return R.error();
	}
	
	@ApiOperation("获取某表中所有列详情")
	@ResponseBody
	@GetMapping("/columns/{oracleSourceId}/{owner}/{tableName}")
	public R getColumns(@PathVariable Integer oracleSourceId, @PathVariable String owner, @PathVariable String tableName) {
		List<ColumnProperty> columns = maskOracleService.getColumns(oracleSourceId, owner, tableName);
		if (columns != null) {
			R r = new R();
			r.put("columns", columns);
			return r;
		}
		
		return R.error();
	}
	
	
}
